package com.zhangqi.zhxa.base.impl.menudetail;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.jeremyfeinstein.slidingmenu.lib.CustomViewAbove;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.viewpagerindicator.TabPageIndicator;
import com.zhangqi.zhxa.MainActivity;
import com.zhangqi.zhxa.R;
import com.zhangqi.zhxa.base.BaseMenuDetailPager;
import com.zhangqi.zhxa.domain.NewsMenuData;

import java.util.ArrayList;

/**
 * 菜单详情页-新闻
 * @author ZhangQi
 * @date 2016-5-2
 */
public class NewsMenuDetailPager extends BaseMenuDetailPager implements CustomViewAbove.OnPageChangeListener, ViewPager.OnPageChangeListener {

    @ViewInject(R.id.vp_news_detail)
    private ViewPager mViewPager;

    @ViewInject(R.id.indicator)
    private TabPageIndicator mIndicator;//页面指示器

    private ArrayList<NewsMenuData.NewsTabData> mTabList;//页签网络数据集合
    private ArrayList<TabDetailPager> mTabPagers;//页签页面集合

    public NewsMenuDetailPager(Activity activity,
            ArrayList<NewsMenuData.NewsTabData> children) {
        super(activity);
        mTabList = children;
    }

    @Override
    public View initView() {
        /*TextView view = new TextView(mActivity);
        view.setText("菜单详情页-新闻");
        view.setTextColor(Color.RED);
        view.setTextSize(22);
        view.setGravity(Gravity.CENTER);*/

        View view = View.inflate(mActivity, R.layout.pager_menu_detail_news,null);
        ViewUtils.inject(this,view);
        return view;
    }

    @Override
    public void initData() {
        //初始化12个页签
        mTabPagers = new ArrayList<TabDetailPager>();
        for (NewsMenuData.NewsTabData tabData:mTabList) {
            //创建一个页签对象
            TabDetailPager pager = new TabDetailPager(mActivity,tabData);
            mTabPagers.add(pager);
        }

        mViewPager.setAdapter(new NewsMenuAdapter());

        mViewPager.setOnPageChangeListener(this);
        //此方法在viewpager设置完数据之后再调用
        mIndicator.setViewPager(mViewPager);//将页面指示器和viewpager关联


    }

    @Override
    public void onPageScrolled(int position, float positionOffset,
                               int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if(position==0){//在第一个页签，允许侧边栏出现
                //开启侧边栏
                setSlidingMenuEnable(true);
            }else{//其他页签，禁用侧边栏，保证viewpager可以正常向右滑动
                //关闭侧边栏
                setSlidingMenuEnable(false);
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    /**
     * 设置侧边栏可用不可用
     * @param enable
     */
    private void setSlidingMenuEnable(boolean enable){
        MainActivity mainUI = (MainActivity) mActivity;
        SlidingMenu slidingMenu = mainUI.getSlidingMenu();//获取侧边栏

        if(enable){
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//全屏点击侧拉框
        }else {
            //禁用掉侧边栏滑动效果
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);//关闭全屏
        }
    }



    class NewsMenuAdapter extends PagerAdapter{

        //返回页面指示器的标题
        @Override
        public CharSequence getPageTitle(int position) {

            return mTabList.get(position).title;
        }

        @Override
        public int getCount() {
            return mTabPagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TabDetailPager pager = mTabPagers.get(position);
            container.addView(pager.mRootView);
            pager.initData();//初始化数据

            return pager.mRootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

}
